package leetCode.offer41;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MediaFinder1 implements MediaFinder{

    List<Integer> list ;

    public MediaFinder1(){
        list = new ArrayList<>();
    }

    @Override
    public void addNum(int num) {
        list.add(num);
    }

    @Override
    public double findMedian() {
        Collections.sort(list);
        double result;
        if(list.size()%2==0){
            result = ((double)list.get(list.size()/2)+list.get(list.size()/2-1))/2;
        }else{
            result = list.get(list.size()/2);
        }
        return result;
    }

    public static void main(String[] args) {
        MediaFinder mediaFinder = new MediaFinder1();
        mediaFinder.addNum(1);
        mediaFinder.addNum(2);
        System.out.println(mediaFinder.findMedian());
    }
}
